define(function (require,exports,module) {
    var myPub=require('./public');
    var oWindow=$(window);
    var oFootBtn=$('.js_footerBtn');
    var oCopyBtn = $('.js_copyBtn');
    // var oCopytext = $('.js_copytext');
    var oBossInfo = $('.js_bossInfo');              //商家信息 js_bossInfo
    var oBossName = $('.js_bossName');              //店家名字
    var clipboard = new Clipboard('.js_copyBtn');
    var oLoadInv = $('.js_loadInv');
    var oGoToStoreDom = $('.js_goToStore');         // 去商家店铺页面的节点
    var oShowNum = $('.js_showNum');                // 显示手机号码眼睛
    var oPhoneNum = $('.js_phoneNum');              // 全部手机号码
    var oHidePhoneNUm = $('.js_hidePhoneNUm');      // 隐藏手机号码
    var nPhoneNum = oPhoneNum.text();
    var nHideNum = nPhoneNum.substr(0,3)+'****'+nPhoneNum.substr(7,11);

    var storeDetailUrl = '/m/g/supplier/1.0/isOverSeaSupplier';        // 判断是进入海外店铺还是进入普通店铺的接口

    var imgUrl = $('.js_lazyImg').attr('src');


    module.exports={
        init:function () {
            orderDetail.orderDetInit(true);
            orderDetail.giftOrderTimerInit();
        }
    }

    // 手机号码隐藏
    oHidePhoneNUm.text(nHideNum);
    oShowNum.hammer().on('tap',function () {
        if($(this).hasClass('on')){
            oPhoneNum.hide();
            oHidePhoneNUm.show();
            $(this).removeClass('on icon-eye_look').addClass('icon-eye_close')
        }else{
            oHidePhoneNUm.hide();
            oPhoneNum.show();
            $(this).addClass('on icon-eye_look').removeClass('icon-eye_close')
        }
    });

    // 点击复制
    oCopyBtn.hammer().on('tap', function () {
        // orderDetail.oCopy(oCopytext)
        // myPub.TipLayer('copy Successfully');
        clipboard.on('success',function (e) {
            myPub.TipLayer('复制成功');
            // console.log(e);
        });
        clipboard.on('error',function (e) {
            myPub.TipLayer('复制错误，请稍后重试');
            // console.log(e);
        })
    });

    // 点击下载
    oLoadInv.hammer().on('tap', function () {
        var url_arr=$(this).attr("urls").replace("[","").replace("]","").split(",");
        var url=url_arr.pop();
        window.location.href = url;

        /*var triggerDelay = 300;
        var removeDelay = 1000;
        var url_arr=$(this).attr("urls").replace("[","").replace("]","").split(",");
        url_arr.forEach(function(item,index){
            _createIFrame(item, index * triggerDelay, removeDelay);
        })
        function _createIFrame(url, triggerDelay, removeDelay) {
            //动态添加iframe，设置src，然后删除
            setTimeout(function() {
                // var frame = $('<iframe style="display: none;" class="" type=""></iframe>');
                // frame.attr('src', url);
                // $(document.body).after(frame);
                // setTimeout(function() {
                //     frame.remove();
                // }, removeDelay);
                window.location.href = url;
            }, triggerDelay);

        }*/

    });

    oGoToStoreDom.hammer().on('tap', function () {
        var supplierId = $(this).attr('data-supplierId');
        orderDetail.goToStoreDetail(supplierId);
    });

    var orderDetail={
        orderDetInit:function (bOff) {
            var orderSnNo = orderSn;                    // 订单编号
            var orderFullName = orderName;              // 收货人名称
            var orderImageId = orderImage;              // 身份证照
            var orderIndCodeNo = orderIndCode;          // 身份证号码
            sessionStorage.setItem('orderSnOdd',escape(orderSnNo));
            sessionStorage.setItem('orderNameOdd',escape(orderFullName));
            sessionStorage.setItem('orderImageOdd',escape(orderImageId));
            sessionStorage.setItem('orderIndCodeOdd',escape(orderIndCodeNo));
            orderDetail.btnAskLayer();
        },
        btnAskLayer:function () {
        //    底部的点击事件绑定给底部的大盒子
        //     console.log(oFootBtn);
            oFootBtn.hammer().on('tap',function (e) {
                var _btnA=$(e.gesture.target);
                var _typeNum=_btnA.attr('_type');

                if(_typeNum!==undefined){
                    myPub.HamstopPropaga();
                }
            	var oId = _btnA.attr("oId");

                switch (_typeNum*1){
                //    0 确认送达
                    case 0:
                    	var _cont = '<p class="delTipP" >亲！ 确定您已收到货物了吗？</p><span class="delTipSpan">确认收货后平台将会将货款打给商家</span>';
                        
                        myPub.HamstopPropaga();
                        
                        myPub.askLayer(_cont,function() {
                            //确定收货
                            G_JSON_GET('/member/order/receive',{"orderId": oId},function(data){
                            	if (data.status == "success") {
                                    //location.reload();
                                    // window.location.href = '/shop/goodsComment/comment/'+oId;
                                    window.location.href= "/h/goodsafter/applyAppraisal/" + oId + '?picImgUrl=' + imgUrl;
                        		} else {
                        			myPub.TipLayer(data.message);
                        		}
                        	});
                            
                            myPub.LayerCloseAll();
                        },function() {
                        },'no');
                        break;
                    //  1 申请退款
                    case 1:
                    	var _cont = '<p class="delTipP" >确认申请退款吗？</p>';
                        var _this = _btnA;
                        myPub.HamstopPropaga();

                        myPub.askLayer(_cont,function() {
                            // 跳转售后
                            var orderId = _this.attr("oId");
                            window.location.href = '/shop/workOrder/showAfterSales/'+orderId+'?isRefund=true';
                            myPub.LayerCloseAll();

                        },function() {
                            //alert('取消了');

                        },'no');
                        break;
                //        2 删除订单
                    case 2:
                    	var _cont = '<p class="delTipP" >亲！ 您确定要删除此订单吗？</p><span class="delTipSpan">删除后此订单会永远消失</span>';
                        myPub.askLayer(_cont,function() {
                            // 判断删除店家
                        	 G_JSON_GET('/member/order/delete',{"orderId": oId},function(data){
                             	if (data.status == "success") {
                             		window.location.href="/shop/pay/order_list";
                         		} else {
                         			myPub.TipLayer(data.message);
                         		}
                         	});
                            myPub.LayerCloseAll();
                        },function() {
                        },'no');
                        break;
                //        3 评价晒单
                    case 3:
                        window.location.href= "/h/goodsafter/applyAppraisal/" + oId + '?picImgUrl=' + imgUrl;
                        // window.location.href= "/shop/goodsComment/comment/"+oId;
                    	//先验证是否已经评价过
//                    	G_JSON_GET('/shop/goodsComment/find_comment/'+oId, null, function (data) {
//                            if (data.status == "success") {
//                            	window.location.href= "/shop/goodsComment/comment/"+oId;
//                            } else {
//                                myPub.TipLayer(data.message);
//                            }
//                        });
                        break;
                //        4 联系商家 
                    case 4:
                    	var styleTitle = 'width:100%;  border:none';
                        var _sBossInfo= $('.js_bossInfo');
                        var _index = oBossInfo.index(_sBossInfo);
                        var _BossName = oBossName.eq(_index).text();
                        
                        myPub.HamstopPropaga();
                        
                        _sBossInfo.show();
                        myPub.TipTitleLayer('联系商家: '+_BossName,styleTitle, _sBossInfo.html(),function() {
                            _sBossInfo.hide();
                        });
                        break;
                //        5 先行赔付
                    case 5:
                    	var _cont = '<p class="delTipP" >确认申请闪赔吗？</p>';
                        var _this = _btnA;
                        myPub.HamstopPropaga();

                        myPub.askLayer(_cont,function() {
                            // 跳转售后
                            var orderId = _this.attr("oId");
                            window.location.href = '/shop/workOrder/showPreRefund/'+orderId;
                            myPub.LayerCloseAll();

                        },function() {
                            //alert('取消了');

                        },'no');
                        break;
                //        6 取消订单
                    case 6:
                        myPub.askLayer(orderDetail.cancleCauseHtml(),function () {
                        //    yesFn
                        },function () {
                        //    noFn
                        },'',function () {
                        //    callback
                        },["退出","确认"]);
                        //改变两个按钮的属性，让“确认”的type为1，让“退出”的type为0
                        $('.layermbtn').find('span').eq(0).attr('type','1');
                        $('.layermbtn').find('span').eq(1).attr('type','0');
                        //改变元素的默认样式
                        $('.layermcont').css({"padding":"0 1.15rem"})
                        $('.layermbtn').css({'height':'3.42rem','line-height':'3.42rem'})
                        $('.layermbtn').find('span').css({'background':'#fff','color':'#838383','font-size':'1.15rem','height':'100%'})
                        $('.layermbtn').find('span').eq(0).css({'box-sizing':'border-box','border-right':'1px solid #dedede'});
                        //绑定具体原因的点击事件
                        orderDetail.causeChangeColor();
                        break;
                //        7 去付款
                    case 7:
                    	submitPay(oId);
                        break;
                //        8 申请售后
                    case 8:
                    	var _cont = '<p class="delTipP" >申请售后前请与商家协商好赔付金额?</p>';
                        var _this = _btnA;
                        myPub.HamstopPropaga();
                        
                        myPub.askLayer(_cont,function() {
                            // 跳转售后
                            // var orderId = _this.attr("oId");
                            // window.location.href = '/shop/workOrder/showAfterSales/'+orderId;

                            //联系商家
                            /*if(jswebcallcount != '' && jswebcallcount != null){
                                if(jswebcallcount == 'sfddj'){
                                    window.location.href = "http://sf.365webcall.com/chat/ChatWin3.aspx?settings=mw7mXbNN0m66066mz3Am66066mz3Am66066bz3Am6mmw0&LL=0"+jswebcallparam;
                                }else{
                                    window.location.href = "http://sf.365webcall.com/chat/ChatWinCorp.aspx?email="+jswebcallcount+"&accountid=sfddj&LL=0"+jsorderwebcallparam;
                                }
                                myPub.LayerCloseAll();
                            }*/
                            if(settingid != '' && settingid != null){
                                $('#xiaonengA').click();
                                myPub.LayerCloseAll();
                            } else {
                                var styleTitle = 'width:100%;  border:none';
                                var _sBossInfo= $('.js_bossInfo');
                                var _index = oBossInfo.index(_sBossInfo);
                                var _BossName = oBossName.eq(_index).text();

                                myPub.HamstopPropaga();

                                _sBossInfo.show();
                                myPub.TipTitleLayer('联系商家: '+_BossName,styleTitle, _sBossInfo.html(),function() {
                                    _sBossInfo.hide();
                                });
                            }

                            //myPub.LayerCloseAll();

                        },function() {
                            //alert('取消了');
                            // 跳转售后
                            var orderId = _this.attr("oId");
                            window.location.href = '/shop/workOrder/showAfterSales/'+orderId;
                            myPub.LayerCloseAll();

                        },'no',null,['联系商家','申请售后']);
                        break;
                    // 送礼订单 分享
                    case 9:
                        window.location.href = "/shop/gift/sharePage/" + oId;
                        break;
                //        10 发表追评
                    case 10:
                    	window.location.href= "/shop/goodsComment/appendComment/"+oId;
                        break;
                    //售后-购物返现以及余额抵扣
                    case 11:
                        var _cont = '<p class="delTipP" >确认申请售后吗?</p>';
                        var _this = _btnA;
                        myPub.HamstopPropaga();
                        myPub.askLayer(_cont,function() {
                            //联系商家
                            /*if(jswebcallcount != '' && jswebcallcount != null){
                                if(jswebcallcount == 'sfddj'){
                                    window.location.href = "http://sf.365webcall.com/chat/ChatWin3.aspx?settings=mw7mXbNN0m66066mz3Am66066mz3Am66066bz3Am6mmw0&LL=0"+jswebcallparam;
                                }else{
                                    window.location.href = "http://sf.365webcall.com/chat/ChatWinCorp.aspx?email="+jswebcallcount+"&accountid=sfddj&LL=0"+jsorderwebcallparam;
                                }
                                myPub.LayerCloseAll();
                            }*/
                            if(settingid != '' && settingid != null){
                                $('#xiaonengA').click();
                                myPub.LayerCloseAll();
                            } else {
                                var styleTitle = 'width:100%;  border:none';
                                var _sBossInfo= $('.js_bossInfo');
                                var _index = oBossInfo.index(_sBossInfo);
                                var _BossName = oBossName.eq(_index).text();

                                myPub.HamstopPropaga();

                                _sBossInfo.show();
                                myPub.TipTitleLayer('联系商家: '+_BossName,styleTitle, _sBossInfo.html(),function() {
                                    _sBossInfo.hide();
                                });
                            }
                        },function() {
                            // 跳转售后
                            var orderId = _this.attr("oId");
                            window.location.href = '/shop/workOrder/showAfterSales/'+orderId;
                            myPub.LayerCloseAll();

                        },'no',null,['联系商家','申请售后']);
                        break;
                    default:
                        ;
                        break;
                }
            })
        },
        cancleCauseHtml:function () {
            var shtml='';
            shtml='<div class="cancleOrderCause js_cancleOrderCause">'+
                    '<div class="cancleReason backWhite">'+
                        '<div class="cancleTit borBEA">'+
                            '<h6>请选择取消原因</h6>'+
                        '</div>'+
                        '<div class="cancleRealReason">'+
                            '<div class="selectReason js_selectReason">'+
                                '<p class="Bflex1">不想买了</p>'+
                                '<p class="Bflex1">拍错/拍多/地址错误</p>'+
                                '<p class="Bflex1">商家缺货</p>'+
                                '<p class="Bflex1">买错了</p>'+
                                '<p class="Bflex1">其他</p>'+
                            '</div>'+
                            '<div class="writeReason">'+
                                '<textarea name="" id="" cols="30" rows="9" placeholder="具体原因（选填）"></textarea>'+
                            '</div>'+
                        '</div>'+
                    '</div>'+
                '</div>';
            return shtml;
        },
        causeChangeColor:function () {
            $('.js_selectReason p').on('click',function () {
                var index=$('.js_selectReason p').index($(this));
                // console.log(index);
                $('.js_selectReason p').removeClass('active').eq(index).addClass('active');
            })
        },
        // 判断是进入海外店铺还是进入普通店铺的接口
        goToStoreDetail: function (supplierId) {
            var _this = this;
            G_JSON_POST(storeDetailUrl,{'supplierId':supplierId},function (data) {
                var _data = data.data;
                var _ret = data.ret;
                // console.log('data',_data,_ret);
                if(_ret.code=='0'){
                    if(_data){
                        // 海外馆商家
                        window.location.href =  base +'/h/store/storehome?supplierId='+ supplierId ;
                    }
                    else{
                        // 普通商家
                        window.location.href =  base +'/h/store/normalStore/'+ supplierId ;
                    }
                }
                else{
                    // 如果请求不到就跳转普通商家
                    window.location.href =  base +'/h/store/normalStore/'+ supplierId ;
                }
            })
        },
        // 复制操作
        oCopy: function(obj) {
            obj.select();
            document.execCommand("Copy");
            myPub.TipLayer("复制成功!");
        },
        // 送TA订单 的计时器
        giftOrderTimerInit:function () {
            var _this = this;
            var $giftTimer = $('.jsGiftTimer');
            if($giftTimer.length <= 0){
                return;
            }
            if(orderPayTime == null){
                return;
            }
            var remainSecond = 24 * 3600 - parseInt((new Date().getTime() -  orderPayTime)/1000);
            var timerText = _this.formatTimer(remainSecond);
            $giftTimer.text(timerText);
            var tempTimerInterval = setInterval(function () {
                remainSecond -- ;
                var timerText = _this.formatTimer(remainSecond);
                $giftTimer.text(timerText);
                if(remainSecond <= 0){
                    clearInterval(tempTimerInterval);
                }
            },1000);
        },
        formatTimer:function (timerSecond) {
            var second =  timerSecond%60;
            var min = ((timerSecond - timerSecond%60)/60)%60;
            var honr = (timerSecond - timerSecond%3600)/3600;
            // console.log("%o  %o  %o  %o",timerSecond,honr,min,second);
            if(honr < 10){
                honr = "0" + honr;
            }
            if(min < 10){
                min = "0" + min;
            }
            if(second < 10){
                second = "0" + second;
            }
            var timerText = honr + ":" + min + ":" + second;
            return timerText;
        }
    }
})